package com.dzbh.cdsdriver.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.dzbh.cdsdriver.R;
import com.dzbh.cdsdriver.model.BillBean;

import java.util.List;

/**
 * Created by dongxinguang on 2016/11/19.
 */

public class ProcessingBillsAdapter extends RecyclerView.Adapter<ProcessingBillsAdapter.NewsViewHolder>{
    private Context mContext;
    private List<BillBean> mDataList;
    SparseBooleanArray mCheckStates=new SparseBooleanArray();
    private LayoutInflater mInflater;
    public ProcessingBillsAdapter(Context context) {
        this.mContext = context ;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setmDataList(List<BillBean> mDataList) {
        this.mDataList = mDataList;
        mCheckStates.clear();
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NewsViewHolder(mInflater.inflate(R.layout.processingbill_item,parent,false));
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, int position) {
        BillBean billBean = mDataList.get(position);
        holder.indexTv.setText(String.format(mContext.getString(R.string.newbills_item), (position + 1)));
        holder.addressTv.setText(billBean.getGoal());
        Log.d("dong", "position->" + position);
        holder.checkBox.setTag(position);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int pos =(int)buttonView.getTag();
                if(isChecked){
                    mCheckStates.put(pos,true);
                    //do something
                }else{
                    mCheckStates.delete(pos);
                    //do something else
                }
            }
        });
        holder.checkBox.setChecked(mCheckStates.get(position,false));
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class NewsViewHolder extends RecyclerView.ViewHolder{
        private TextView indexTv;
        private TextView addressTv;
        private CheckBox checkBox;
        public NewsViewHolder(View itemView) {
            super(itemView);
            indexTv = (TextView) itemView.findViewById(R.id.index);
            addressTv = (TextView) itemView.findViewById(R.id.address);
            checkBox = (CheckBox) itemView.findViewById(R.id.check);
        }
    }

    public SparseBooleanArray getmCheckStates() {
        return mCheckStates;
    }
}
